#!/usr/bin/python
# plot_LumFunc.py

import numpy
from matplotlib import use as useBackend
useBackend('Agg')
import matplotlib.pyplot as ppt


def cheat111():
    '''Cheat to clear plot.'''
    ppt.subplot(131)
    ppt.plot([0,1],[1,2])
    ppt.subplot(311)
    ppt.plot([0,1],[1,2])
    ppt.subplot(111)
    ppt.hold(True)


def plot(distriblist, simpars):
    '''Shows a slice of GM_LumFunc's results.'''
    for i in simpars:
        cmd = "%s = simpars['%s']" % (i,i)
        exec cmd
    dmax = int(scale/1000.)
    bstring = 'Galactic latitude'
    dstring = 'Distance from sun, kpc'
    lstring = 'Galactic longitude'
    lims_ld = [0, dmax, 0, 180]
    lims_bd1 = [0, dmax, 0, 90]
    lims_bd2 = [0, dmax, 0, 90]
    asp_ld = 0.6*dmax/180.
    asp_bd = 0.6*dmax/90.
    maxchars = 0
    for i in range(0, len(distriblist)):
        nchars = len(str(MvList[i]))
        if nchars > maxchars:
            maxchars = nchars
#            maxstring = str(MvList[i])
    # Plot slices for each population
    for i in range(0, len(distriblist)):
        stars = distriblist[i]
        magstring = str(MvList[i])
        nchars = len(magstring)
        l = maxchars - nchars
        if magstring.contains('.'):
            magstring = magstring + "0"*l
        else:
            magstring = "0"*l + magstring
        plotnameld = outdir+"Output/Stars"+magstring+"ldPlane.pdf"
        plotnamebd1 = outdir+"Output/Stars"+magstring+"bdPlaneLeeward.pdf"
        plotnamebd2 = outdir+"Output/Stars"+magstring+"bdPlaneWindward.pdf"
        plottitleld = 'Density of stars with magnitude '+magstring+'''
in the galactic plane'''
        plottitlebd1 = 'Density of stars with magnitude '+magstring+'''
away from the galactic center''')
        plottitlebd2 = 'Density of stars with magnitude '+magstring+'''
towards the galactic center''')

        cheat111()
        ppt.imshow(stars[:,:,0].T, origin='lower', extent=lims_ld, aspect=asp_ld)
        ppt.title(plottitleld)
        ppt.colorbar(shrink=0.8)
        ppt.xlabel(dstring)
        ppt.ylabel(bstring)
        ppt.savefig(plotnameld)

        cheat111()
        ppt.imshow(stars[:,0,:].T, origin='lower', extent=lims_bd1, aspect=asp_bd)
        ppt.title(plottitlebd1)
        ppt.colorbar(shrink=0.8)
        ppt.xlabel(dstring)
        ppt.ylabel(bstring)
        ppt.savefig(plotnamebd1)

        cheat111()
        ppt.imshow(stars[:,-1,:].T, origin='lower', extent=lims_bd2, aspect=asp_bd)
        ppt.title(plottitlebd2)
        ppt.colorbar(shrink=0.8)
        ppt.xlabel(dstring)
        ppt.ylabel(bstring)
        ppt.savefig(plotnamebd2)



    brightstars = distriblist[0]
    dimstars = distriblist[-1]
    dmax = PtScaled*dlocs[-1]
    dmax = int(dmax/100.)/10. # in kpc, w/ single decimal point
    dlabel = str(dmax)+" kpc <- d <- 0 kpc"
    blabel = '0 -> b -> $\pi$/2'
    llabel = '0 -> l -> $\pi$'
    # Bright stars
    cheat111()
    ppt.imshow(brightstars[:,:,0])
    ppt.title("Density of intrinsically bright stars for b ~ 0")
    ppt.colorbar()
    ppt.xlabel(llabel)
    ppt.ylabel(dlabel)
    plotname = outdir+'BrightStarsldPlane.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(brightstars[:,0,:])
    ppt.title("Density of intrinsically bright stars for l ~ 0")
    ppt.colorbar()
    ppt.xlabel(blabel)
    ppt.ylabel(dlabel)
    plotname = outdir+'BrightStarsbdPlaneLeeward.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(brightstars[:,-1,:])
    ppt.title("Density of intrinsically bright stars for l ~ pi")
    ppt.colorbar()
    ppt.xlabel(blabel)
    ppt.ylabel(dlabel)
    plotname = outdir+'BrightStarsbdPlaneWindward.pdf'
    ppt.savefig(plotname)

    # Dim stars
    cheat111()
    ppt.imshow(dimstars[:,:,0])
    ppt.title("Density of intrinsically dim stars for b ~ 0")
    ppt.colorbar()
    ppt.xlabel(llabel)
    ppt.ylabel(dlabel)
    plotname = outdir+'DimStarsldPlane.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(dimstars[:,0,:])
    ppt.title("Density of intrinsically dim stars for l ~ 0")
    ppt.colorbar()
    ppt.xlabel(blabel)
    ppt.ylabel(dlabel)
    plotname = outdir+'DimStarsbdPlaneLeeward.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(dimstars[:,-1,:])
    ppt.title("Density of intrinsically dim stars for l ~ pi")
    ppt.colorbar()
    ppt.xlabel(blabel)
    ppt.ylabel(dlabel)
    plotname = outdir+'DimStarsbdPlaneWindward.pdf'
    ppt.savefig(plotname)

